﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Ycjw.PipeLine.Commons
{
    public class Paths
    {
        public static string ApplicationExePath
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().Location;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }
        /// <summary>
        /// AppLicfilePath
        /// </summary>
        public static string AppLicfilePath
        {
            get
            {
                var dir = Path.Combine(AppLocalLowPath, "Jxkd");
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                return dir + "//tmp.lic";
            }
        }

        public static string ApplicationPath
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationExePath);
                return f.Directory.FullName;
            }
        }

        public static string ApplicationPy
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationPath);
                return f.FullName + "/python36/python.exe";
            }
        }

        public static string ApplicationPythonScripts
        {
            get
            {
                return System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "scripts\\";
            }
        }

        public static string ApplicationBinDir
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationExePath);
                return f.Directory.FullName;
            }
        }

        public static string ApplicationConfigDir
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationPath);
                return f.FullName + "/configs";
            }
        }

        public static string ApplicationPluginsDir
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationPath);
                return f.FullName + "/plugins";
            }
        }

        public static string ApplicationResourcesDir
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationPath);
                return f.FullName + "\\resources";
            }
        }

        public static string ApplicationLogDir
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationPath);
                return f.FullName + "\\logs";
            }
        }

        /// <summary>
        /// 临时文件路径
        /// </summary>
        public static string ApplicationTempResources
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationPath);
                string path = f.FullName + "\\temp";
                if (!System.IO.Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                return path;
            }
        }

        /// <summary>
        /// APPLow
        /// </summary>
        public static string AppLocalLowPath
        {
            get
            {
                return Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)), "LocalLow");
            }
        }

        /// <summary>
        /// 系统帮助文档路径
        /// </summary>
        public static string ApplicationHelpDocument
        {
            get
            {
                FileInfo f = new FileInfo(ApplicationPath);
                return f.FullName + "\\document\\Help.pdf";
            }
        }

        internal class FileTimeInfo
        {
            public string FileName;  //文件名
            public DateTime FileCreateTime; //创建时间
            public FileInfo FileInfo;
        }

        /// <summary>
        /// 获取目录下指定后缀的文件
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static List<FileInfo> FilesInDir(string dir,string ext)
        {
            List<FileInfo> list = new List<FileInfo>();
            DirectoryInfo d = new DirectoryInfo(dir);
            foreach (FileInfo fi in d.GetFiles())
            {
                if (string.IsNullOrEmpty(ext) || fi.Extension.ToUpper() == ext.ToUpper())
                {
                    list.Add(fi);
                }
            }
            return list;
        }
        /// <summary>
        /// 获取某个路径下的最新文件
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static FileInfo FileInDirLastCreated(string dir,string ext,string extAnother = null)
        {
            if (!Directory.Exists(dir))
            {
                throw new FileNotFoundException(dir + " 未找到");
            }
            FileTimeInfo fi = GetLatestFileTimeInfo(dir, ext,extAnother);
            return new FileInfo(fi.FileName);
        }
        //获取最近创建的文件名和创建时间
        //如果没有指定类型的文件，返回null
        static FileTimeInfo GetLatestFileTimeInfo(string dir, string ext,string extAnother = null)
        {
            List<FileTimeInfo> list = new List<FileTimeInfo>();
            DirectoryInfo d = new DirectoryInfo(dir);
            foreach (FileInfo fi in d.GetFiles())
            {
                if (string.IsNullOrEmpty(ext) || fi.Extension.ToUpper() == ext.ToUpper())
                {
                    list.Add(new FileTimeInfo()
                    {
                        FileName = fi.FullName,
                        FileCreateTime = fi.CreationTime,
                        FileInfo = fi
                    });
                }
            }
            if(list.Count == 0)
            {
                foreach (FileInfo fi in d.GetFiles())
                {
                    if (string.IsNullOrEmpty(extAnother) || fi.Extension.ToUpper() == extAnother.ToUpper())
                    {
                        list.Add(new FileTimeInfo()
                        {
                            FileName = fi.FullName,
                            FileCreateTime = fi.CreationTime,
                            FileInfo = fi
                        });
                    }
                }
            }
            var qry = from x in list
                      orderby x.FileCreateTime
                      select x;
            return qry.LastOrDefault();
        }
        public static void ClearFolder(string dir)
        {
            foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (System.IO.File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    System.IO.File.Delete(d);//直接删除其中的文件    
                }
                else
                {
                    DirectoryInfo d1 = new DirectoryInfo(d);
                    if (d1.GetFiles().Length != 0)
                    {
                        ClearFolder(d1.FullName);////递归删除子文件夹  
                    }
                    Directory.Delete(d);
                }
            }
        }
    }
}